<?php defined('SYSPATH') or die('No direct script access.');
require ('application/classes/dao/base.php');

require ('application/classes/api/china.php');
/**
 * Created by PhpStorm.
 * User: yinnannan
 * Date: 16/3/7
 * Time: 下午8:28
 */
class Controller_product extends Controller
{
    public function action_getAll(){
        $result = null;
        $parm = get($this->request);
        try{
            if(!isset($parm['productId'])||$parm['productId']==""){
                $result['success'] = false;
                $result['data'] = '产品id不能未空';
            }
            $list = rpcCall('specificationShow.getAllSpec',$parm);
            $html = "";
            $specificationsname = array();
            foreach ($list as  $key =>$value) {
                array_push($specificationsname,$value['specificationsname']);

            }
            $data=array_unique($specificationsname);
            $count = 1;
            foreach($data as  $key =>$value){
              
                $html = $html.$value.':<select  id="model'.$count.'"  name="model'.$count.'">';
                $count ++;
                foreach($list as $keyList =>$valueList){
                    if($valueList['specificationsname'] ==$value){
                        $html=$html.'<option value="'.$valueList['model'].'">'.$valueList['model'].'</option>';
                    }
                }
                $html = $html.'</select><br><input name="uuid" type="hidden" value="'.rand(0,99999999).'"/>';
            }
            echo $html;return ;
        }catch (Exception $e){
            $result['success'] = false;
            $result['data'] = '系统异常稍后尝试';
            $result['error'] = $e->getMessage();

        }finally{


        }
        echo json_encode($result);
    }


    public function action_order(){
        $parm = post($this->request);
        $result = checkUserSessionOrJumpLogin();
        $parm['userId'] = $result['id'];
        $parm['orderId']= date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        $parm['status'] = '订单未支付';
        
        //默认第一个
        $address = rpcCall('userAddress.getByUserId',$parm);
        $express = rpcCall('express.getListExpress',null);
        if(count($address) ==0){
            echo "<script>window.location.href='".Url::base()."index.php/user/address'</script>";
            return false;
        }
        $parm['money'] =-1;
        $parm['addressId'] =$address[0]['id'];
        $parm['expressId'] =$express[0]['id'];

        $isDisplay = 'block';

        $parm['specificationId'][0]= rpcCall('specification.getByModel',$parm);

        $orderList = rpcCall('order.add',$parm);
        $parm['orderId']  =$orderList ;
        $list = call('order.getByOrderId',$parm);
        $china = new china();

        $sizeMoney = countOrder($list);

        $data['money'] = intval($sizeMoney['money']);
        $data['orderId']=$parm['orderId'];
        Call('order.updateMoneyByOrderId',$data);

        $result = $china->getProvince();
        $address = call('userAddress.getByUserId',$parm);
        $express = rpcCall('express.getListExpress',null);
        foreach ($list as $key => $value) {
            $specification = array();
            // echo $value['productId'];
             $specifications = rpcCall('specificationShow.getAllSpec',$value);

            if($value['model1']!=""){
               
                foreach ($specifications as $skey => $svalue) {

                    if($value['model1'] == $svalue['model']){
                        echo  $svalue['model'];
                        echo $svalue['specificationsname'].':'.$value['model1'];
                        array_push($specification,$svalue['specificationsname'].':'.$value['model1']);
                        break;
                    }
                }
            }
            if($value['model2']!=""){
              
                foreach ($specifications as $skey => $svalue) {
                    if($value['model2'] == $svalue['model']){
                        array_push($specification,$svalue['specificationsname'].':'.$value['model2']);
                        break;
                    }
                }
            }
             if($value['model3']!=""){
               
                foreach ($specifications as $skey => $svalue) {
                    if($value['model3'] == $svalue['model']){
                        array_push($specification,$svalue['specificationsname'].':'.$value['model3']);
                        break;
                    }
                }
            }
            $list[$key]['specificationsList']=$specification;
        }
        echo View::factory('web/order')->set('orderId',$parm['orderId'])->set('list',$result)->set("address",$address)->set("sizeMoney",$sizeMoney['money'])->set("orderDetail",$sizeMoney['msg'])->set('express',$express)->set('orderList', $list);
    }

    public function action_test(){
        $parm['orderId'] = '2016030955984856';
        $result = checkUserSessionOrJumpLogin();
        $parm['userId'] = $result['id'];
        $list = rpcCall('order.getByOrderId',$parm);
        $sizeMoney = countOrder($list);
    }
    public function action_checkSku(){
        $parm = post($this->request);

        $parm['id']= rpcCall('specification.getByModel',$parm);
        $result= rpcCall('productexpresssku.getById',$parm);

        if(count($result) == 0){
            echo (0);
            die ;

        }
        if($result[0]['number']-$result[0]['freeze']<$parm['number']){
            echo (0);
            die ;
        }
        echo 1;

    }
    public function action_updateNumber(){
        $result = checkUserSessionOrJumpLogin();
        $parm = post($this->request);
        $parm['userId'] = $result ['id'];
        rpcCall('order.updateNumber',$parm);
        $list = call('order.getByOrderId',$parm);
        $sizeMoney = countOrder($list);
        echo $sizeMoney['money'];
    }

    public function action_updateAddress(){
        $result = checkUserSessionOrJumpLogin();
        $parm = post($this->request);
        $parm['userId'] = $result ['id'];
        rpcCall('order.updateAddress',$parm);
        $list = call('order.getByOrderId',$parm);
        $sizeMoney = countOrder($list);
        echo $sizeMoney['money'];
    }

      public function action_updateExpress(){
        $result = checkUserSessionOrJumpLogin();
        $parm = post($this->request);
        $parm['userId'] = $result ['id'];
        rpcCall('order.updateExpress',$parm);
        $list = call('order.getByOrderId',$parm);
        $sizeMoney = countOrder($list);
        echo $sizeMoney['money'];
    }
}